/*
 * Copyright 2007-2009 Perttu "celeron55" Ahola <celeron55@gmail.com>
 *
 * This file is part of jukumagic.
 *
 * jukumagic is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * jukumagic is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with jukumagic.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "common.h"
#include "narrowcut.h"
#include "jukuapp.h"
#include <iostream>

NarrowCut::NarrowCut() : m_pos(0,0)
{
	//std::cout<<"NarrowCut"<<std::endl;
}

NarrowCut::~NarrowCut()
{
	//std::cout<<"~NarrowCut"<<std::endl;
}

void NarrowCut::WriteXML(TiXmlNode* root)
{
    TiXmlElement *element = new TiXmlElement("NarrowCut");
    root->LinkEndChild(element);
    element->SetAttribute("x", m_pos.m_x);
    element->SetAttribute("y", m_pos.m_y);
	WriteBaseXML(element);
}

JukuObject* NarrowCut::ReadFromXMLElement(TiXmlElement* element)
{
	//std::cout<<"NarrowCut::ReadFromXMLElement()"<<std::endl;

    NarrowCut *narrowcut = new NarrowCut();

	element->Attribute("x", &narrowcut->m_pos.m_x);
	element->Attribute("y", &narrowcut->m_pos.m_y);

	narrowcut->ReadBaseXML(element);

    return narrowcut;
}

void NarrowCut::Draw(wxDC &dc, wxWindow *window, wxPoint origin)
{
	int xadd = m_pos.m_x + origin.x;
	int yadd = m_pos.m_y + origin.y;
	if(GetSelected()){
		wxPen pen(wxGetApp().m_settings.m_colour_selected,
				wxGetApp().m_settings.m_linewidth_narrowcut);
		dc.SetPen(pen);
	}
	else{
		wxPen pen;
		if(GetAutomatic()){
			pen = wxPen(wxGetApp().m_settings.m_colour_automatic,
					wxGetApp().m_settings.m_linewidth_narrowcut);
		}
		else{
			pen = wxPen(wxGetApp().m_settings.m_colour_narrowcut,
					wxGetApp().m_settings.m_linewidth_narrowcut);
		}
		dc.SetPen(pen);
	}
	wxBrush brush(*wxBLACK, wxTRANSPARENT);
	dc.SetBrush(brush);
	dc.DrawLine(50+xadd,-30+yadd, 50+xadd, 30+yadd);
}

BoardRect NarrowCut::GetDrawArea()
{
	return BoardRect(50-30+m_pos.m_x, -70+m_pos.m_y, 60, 140);
}

BoardPoint NarrowCut::GetBoardPos()
{
	return m_pos;
}

void NarrowCut::SetBoardPos(BoardPoint p)
{
	m_pos = p;
}

void NarrowCut::Move(int dx, int dy)
{
	m_pos.m_x += dx;
	m_pos.m_y += dy;
}

bool NarrowCut::IsSelectedInPoint(int x, int y)
{
	return (abs(x - (m_pos.m_x+50)) < 20 && abs(y - m_pos.m_y) < 50);
}

bool NarrowCut::IsSelectedInArea(int x1, int y1, int x2, int y2, bool touch_is_enough)
{
	BoardRect sr = BoardRect(x1, y1, x2-x1, y2-y1);
	BoardRect r(50+m_pos.m_x - 20, m_pos.m_y - 50, 40, 100);
	if(touch_is_enough){
		if(r.Collides(sr)) return true;
	}
	else{
		if(sr.Contains(r)) return true;
	}
	return false;
}

/*void NarrowCut::SetSelected(bool selected)
{
	JukuObject::SetSelected(selected);
}*/

void NarrowCut::Rotate(TurnDirection dir, BoardPoint origin)
{
	int ox = m_pos.m_x - origin.m_x;
	int oy = m_pos.m_y - origin.m_y;
	int nox, noy;
	if(dir==CCW){
		nox = oy;
		noy = -ox;
	}
	else{ //CW
		nox = -oy;
		noy = ox;
	}
	m_pos.m_x = origin.m_x + nox;
	m_pos.m_y = origin.m_y + noy;
}

BoardRect NarrowCut::GetPinExtents()
{
	return BoardRect(m_pos.m_x, m_pos.m_y, 0, 0);
}
